from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import urllib


#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    lis = soup.select(".list-searchResults .result-item")

    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li.select(".title a")[0].text.strip()
        except:
            pass
        try:
            tempData["titleHref"] = 'https://www.jstor.org'+li.select(".title a")[0]["href"]
        except:
            pass
        try:
            tempData["author"] = [li.select(".contrib")[0].text.strip()]
        except:
            pass
        try:
            tempData["source"] = li.select(".src")[0].text.strip()
        except:
            pass
        allData.append(tempData)

    return allData

def getData(keyword,cookies):
    headers = {
        'authority': 'www.jstor.org',
        'pragma': 'no-cache',
        'cache-control': 'no-cache',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'cookie': 'UUID=2c6d7db6-c212-4dfb-8321-d42fd0c9d1d0; csrftoken=qVLD2R8jzd8r4Nmm5t1LbG6OfiWcASCOLaZEOsfQd8BiiPw29wOMC9u3WPYnOUCb; check=true; AMCV_774C31DD5342CAF40A490D44%40AdobeOrg=793872103%7CMCIDTS%7C17927%7CMCMID%7C28716252440322191510435507286954170038%7CMCAAMLH-1549432780%7C11%7CMCAAMB-1549432780%7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y%7CMCAID%7CNONE; _ga=GA1.2.1641409299.1548827988; _gid=GA1.2.171371882.1548827988; _gat_UA-98658597-3=1; s_cc=true; tp=8566; pxvid=3ad744f0-2454-11e9-b1bc-cf885aba9d97; _pxvid=3ad744f0-2454-11e9-b1bc-cf885aba9d97; s_ppv=search%2520results%2C10%2C6%2C853; _px2=eyJ1IjoiNDg0OTU4ZDAtMjQ1NC0xMWU5LWI5ZTItODkzNDUxZmQ5ZTNiIiwidiI6IjNhZDc0NGYwLTI0NTQtMTFlOS1iMWJjLWNmODg1YWJhOWQ5NyIsInQiOjE1NDg4MjgyODIxMTksImgiOiIxZDg4ZDFlNzhkNmIzMWE4MDM4ZjNkZmQ0MTEwYWM0NTIxMmJhZWIxMmQ5OTgwMDAxNmIxOGE2NmZkMzNiY2JmIn0%3D; _starUuid=8d525c5c-98a5-40e5-a77d-a079e86dfea1; AccessSession=H4sIAAAAAAAAAJ2RSW_bMBCF7_kVgs6hweE6zM12tqLH5FYUwYikYrVKbGgpkAT577VJKa67XHrk92YeHx_fzoqibEJZXBRlRIwCIsTAQWn0FRofVCV4qOrokMrzw7CfphvyjIi4zfjbhKWTsDJyvUIHZrUEh_xam6urSyONU9cyT3fTdB-p85uLWkYhubEKyQQhpPIEKCyIaK2zNeQlGofNYaumto-J_KA2G4FWiMIhKIsmSc0uXQDgFiDtQqsFoJt8wlPzfGrU99tTMPjTM3k_9Af0pXjbn4-lAec8--7ZlKZUM4gTcE5VUjrNakeaKSc4Q6TIhAs1AvpaBpp3hpddTEs33XbcfVh_4BX1jc_aXno__0sg-Xsg8UegiNyG6FktbcWUt5qRR8G439dE2lec-H8GKr7mSofjx1intXA88ZZ-EZAff6wd_yHQMHSp--mh63W6dnk3R1nfJ_A40vPj62Y7PtxcztJdlj7d3y4_L2dYje33TaTcxNCNMeU-e_8Ji5TrZQ0DAAA; AccessSessionSignature=b1a0a6ae0b384acc3e817c912b61778d289c072a416beafeb582af6cb3ff08d1; AccessSessionTimedSignature=f47476d193c8d45f79ad8495e79275267a0e34a394727e9804545ba0e7ce43a2; ADRUM_BT=R:0|g:d604d050-3990-4d51-9796-fa08ec3aa2d3|e:1175|n:Ithaka_20fb118f-0305-4128-b6c9-ee5f3357d8aa|i:128931; mbox=session#c4fa868b06b540568ffa117a93f824ca#1548829900|PC#1be20af7e5174eaa80b40414ad05e424.22_34#1612072840',
        'Referer': 'https://www.jstor.org/action/doBasicSearch?Query=cell&acc=on&wc=on&fc=off&group=none',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'referer': 'https://www.jstor.org/action/doBasicSearch?Query=cell&acc=on&wc=on&fc=off&group=none',
        'Origin': 'https://www.jstor.org',
        'Intervention': '<https://www.chromestatus.com/feature/5718547946799104>; level="warning"',
        'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
        'Pragma': 'no-cache',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Connection': 'keep-alive',
        'Cache-Control': 'no-cache',
    }

    params = (
        ('Query', keyword),
        ('acc', 'on'),
        ('wc', 'on'),
        ('fc', 'off'),
        ('group', 'none'),
    )

    response = requests.get('https://www.jstor.org/action/doBasicSearch', headers=headers, params=params,
                            cookies=cookies)

    allData = formData(response)

    return allData

def getCookies():
    headers = {
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    }
    s = requests.session()
    response = s.get('http://www.jstor.org/', headers=headers)
    cookies = s.cookies.get_dict()
    return cookies


def index(request):


    # page = 1
    data = json.loads(request.POST.get('data', '1'))

    cookies = getCookies()

    allData= getData(data["values"][0],cookies)

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")